#!/bin/bash

# 配置
APP_NAME="wbApp:app"  # Flask 应用名，格式为 module:app
BIND_ADDRESS="0.0.0.0:9001"  # Gunicorn 绑定地址和端口
WORKERS=4  # Gunicorn 工作进程数
PID_FILE="/tmp/gunicorn.pid"  # PID 文件路径

# 停止与 APP_NAME 相关的 Gunicorn 进程
echo "Looking for existing Gunicorn process related to $APP_NAME..."

PIDS=$(ps aux | grep "gunicorn" | grep "$APP_NAME" | grep -v "grep" | awk '{print $2}')

if [ -n "$PIDS" ]; then
    for PID in $PIDS; do
        echo "Stopping existing Gunicorn process (PID: $PID)..."
        kill "$PID"
        echo "Process $PID stopped."
    done
else
    echo "No Gunicorn process for $APP_NAME found."
fi

# 启动新的进程
echo "Starting Gunicorn..."
nohup gunicorn -w "$WORKERS" -b "$BIND_ADDRESS" "$APP_NAME"  &

echo "Gunicorn started "
